package main

import "fmt"

// 最大值--值传递
func max(a int, b int) int {
	fmt.Println()
	fmt.Println("=========max========")
	fmt.Println()
	if a > b {
		return a
	}
	return b
}

// 交换--值传递
func swap(a string, b string) (string, string) {
	fmt.Println()
	fmt.Println("=========swap========")
	fmt.Println()
	return b, a
}

// 交换的test
func newSwap() {
	fmt.Println()
	fmt.Println("=========newSwap========")
	fmt.Println()
	var a, b = 1, 2
	fmt.Printf("swap之前a = %d, b = %d\n", a, b)
	swapAdd(&a, &b)
	fmt.Printf("swap之前a = %d, b = %d\n", a, b)
}

// 交换--引用传递
func swapAdd(a *int, b *int) {
	temp := *a
	*a = *b
	*b = temp
}

// 函数作为变量传递
func funcChildFunc() {
	fmt.Println()
	fmt.Println("=========funcChildFunc========")
	fmt.Println()
	// 已存在函数作为变量传递
	childFunc := swapAdd
	var a, b = 1, 2
	fmt.Printf("swap之前a = %d, b = %d\n", a, b)
	childFunc(&a, &b)
	fmt.Printf("swap之前a = %d, b = %d\n", a, b)

	// 新写的函数作为变量传递
	childFuncNew := func(x int) int {
		return x + 1
	}
	fmt.Println(childFuncNew(1))
}

// 闭包函数
func closureFunc() func() int {
	fmt.Println()
	fmt.Println("=========closureFunc========")
	fmt.Println()
	i := 0
	return func() int {
		i++
		return i
	}
}

// 闭包函数可以重复使用，重复重制
func useClosureFunc() {
	nextNumber := closureFunc()
	fmt.Println(nextNumber())
	fmt.Println(nextNumber())
	fmt.Println(nextNumber())

	newNextNumber := closureFunc()
	fmt.Println(newNextNumber())
	fmt.Println(newNextNumber())
}
